package com.kejso.controller;

import com.kejso.model.db.Scholar;
import com.kejso.model.solr.SolrScholar;
import com.kejso.service.ScholarService;
import com.kejso.util.SortUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by DELL on 2018/4/23.
 */
@Controller
public class MainController {

    @Autowired
    ScholarService scholarService;

    @RequestMapping(value = "/",method = RequestMethod.GET)
    public Object main(){
        return "main";
    }

    @RequestMapping(value = "/searchPersonByName",method = RequestMethod.GET)
    public Object searchPersonByName(HttpServletRequest request,Model model){
        String[] args = request.getParameter("name").split(" ");
        if(args.length==1) {
            List<SolrScholar> scholars = scholarService.findByName(args[0]);
            model.addAttribute("scholars", scholars);
        } else {
            List<SolrScholar> scholars = scholarService.findByNameAndOrg(args[0],args[1]);
            model.addAttribute("scholars", scholars);
        }
        return "main";
    }

    @RequestMapping(value = "/modifyForm",method = RequestMethod.GET)
    public Object modifyForm(HttpServletRequest request,Model model){
        String id = request.getParameter("id");
        SolrScholar scholar = scholarService.findById(id);
        if(scholar.getPrize()==null)
            scholar.setPrize("");
        scholar.initScores();
        model.addAttribute("scholar",scholar);
        return "modify_form";
    }

    @RequestMapping(value="/delete",method=RequestMethod.GET)
    public Object delete(HttpServletRequest request,Model model) {
        String id = request.getParameter("id");
        SolrScholar solrScholar = scholarService.findById(id);
        scholarService.delete(id);

        String URL = "http://10.2.16.7:8000/faketest/delete/?id=" + solrScholar.getId();
        RestTemplate restTemplate = new RestTemplate();
        restTemplate.getForObject(URL,Object.class);

        List<SolrScholar> scholars = scholarService.findByNameAndOrg(solrScholar.getName(),solrScholar.getOrg());
        model.addAttribute("scholars", scholars);
        return "main";
    }

    @RequestMapping(value = "/modify",method = RequestMethod.POST)
    public Object modify(HttpServletRequest request,Model model){
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String org = request.getParameter("org");
        String label = request.getParameter("label");
        Double baseScore = Double.parseDouble(request.getParameter("baseScore"));

        String achievement = request.getParameter("achievement");
        String experience = request.getParameter("experience");
        String prize = request.getParameter("prize");

        String[] fields = request.getParameterValues("field");
        String[] fieldWeights = request.getParameterValues("fieldWeight");
		List<SolrScholar> solrScholarList = new ArrayList<>();
        if(request.getParameter("a").equals("确定修改")) {
            SolrScholar sc = scholarService.findById(id);
            solrScholarList.add(sc);
        } else if(request.getParameter("a").equals("全部修改")) {
            solrScholarList = scholarService.findByNameAndOrg(name,org);
        }

        for(SolrScholar solrScholar:solrScholarList){

            solrScholar.setBaseScore(baseScore);
            solrScholar.setLabel(label);
            solrScholar.setAchievement(achievement);
            solrScholar.setExperience(experience);
            solrScholar.setPrize(prize);

            List<String> fieldList = new ArrayList<>();
            List<Double> fieldWeightList = new ArrayList<>();
            String fieldsStr = "";
            String fieldWeightsStr = "";
            for(int i=0;fields!=null && fieldWeights!=null && i<fields.length && fieldWeights.length == fields.length;i++){
                if(fields[i] == null || fields[i].equals("")){
                    continue;
                }
                fieldList.add(fields[i]);
                fieldWeightList.add(Double.parseDouble(fieldWeights[i]));
            }

            Map<String,Double> map = SortUtil.sort(fieldList,fieldWeightList);
            List<String> fields_new = new ArrayList<>();
            List<Double> fieldWeights_new = new ArrayList<>();
            for (Map.Entry<String, Double> entry : map.entrySet()) {
                fields_new.add(entry.getKey());
                fieldWeights_new.add(entry.getValue());
            }
            if(fields_new.size() == fieldWeights_new.size() && fields_new.size() !=0){
                fieldList = fields_new;
                fieldWeightList = fieldWeights_new;
            }

            for(int i=0;i<fieldList.size() && fieldWeightList.size() == fieldList.size();i++){
                fieldsStr = fieldsStr + fieldList.get(i) +";";
                fieldWeightsStr = fieldWeightsStr + fieldWeightList.get(i) +";";
            }

            solrScholar.setFields(fieldList);
            solrScholar.setFieldsWeights(fieldWeightList);

            Scholar scholar = new Scholar();
            scholar.setBase_score(baseScore);
            scholar.setId(solrScholar.getId());
            scholar.setFields(fieldsStr);
            scholar.setFields_weight(fieldWeightsStr);
            scholar.setLabel(label);
            scholar.setAchievement(achievement);
            scholar.setExperience(experience);
            scholar.setPrize(prize);

            scholarService.updateScholarSolr(solrScholar);
            System.out.println("修改solr成功");
            scholarService.updateScholarDB(scholar);
            System.out.println("修改mysql成功");

            String URL = "http://10.2.16.7:8000/faketest/update/?id=" + solrScholar.getId();
            RestTemplate restTemplate = new RestTemplate();
            restTemplate.getForObject(URL,Object.class);
        }

        SolrScholar solrScholar = scholarService.findById(id);
        solrScholar.initScores();
        if(solrScholar.getPrize()==null)
            solrScholar.setPrize("");

        model.addAttribute("scholar",solrScholar);
        return "modify_form";
    }
}
